function upMat = updatematrix(bParams,oParams,setts)
% Calculate updating matrix, which is used to speed up equity value calculations

% Variables:
% bParams,oParams,setts		see readme

% Set parameters
	pbar = oParams.pbar;
	n = oParams.n;

	gamma = bParams.gamma;
	beta = bParams.beta;
	theta= bParams.theta;
    
	lambda_grid = setts.lambda_grid;
	n_lambda = setts.n_lambda;

% Set initial values
	G = zeros(n_lambda*(pbar+1),pbar+1);
	LG = zeros(n_lambda*(pbar+1),pbar+1);
	Q = zeros(n_lambda*(pbar+1),1);
	L = zeros(n_lambda*(pbar+1),1);
	prob_matrix = binopdf((0:n),n,theta);

% Create updating matrix
	for i = 1:n_lambda
		for l = 0:pbar
			G(i+l*n_lambda,l+1:min(l+1+n,pbar+1)) = prob_matrix(1:min(n+1,pbar-l+1));
			G(i+l*n_lambda,end) = G(i+l*n_lambda,end) + 1 - sum(G(i+l*n_lambda,l+1:end));
			G(i+l*n_lambda,l+1) = G(i+l*n_lambda,l+1) - 1;
			Q(i+l*n_lambda) = q(l,lambda_grid(i),gamma,beta);
			L(i+l*n_lambda) = lambda_grid(i);
			LG(i+l*n_lambda,:) = L(i+l*n_lambda) * G(i+l*n_lambda,:);
		end
	end

% Calculate innovation probabilities
    bin_prob = zeros(pbar+1,pbar+1);
        for i = 0:pbar
            for j = 0:min(pbar-i,n)
                bin_prob(i+1,i+1+j) = binopdf(j,n,theta);
			end
			if i+n>pbar
				bin_prob(i+1,pbar+1) = bin_prob(i+1,pbar+1) + (1-sum(bin_prob(i+1,:)));
			end
		end

% Save results in upMat
	upMat.Q = Q;
	upMat.LG = LG;
	upMat.L = L;
    upMat.bin_prob = bin_prob;

end

